<template>
  <div>
    <van-nav-bar
      :z-index="999"
      :fixed="true"
      left-arrow
      @click-left="onClickLeft"
      :placeholder="true"
      :title="type"
    ></van-nav-bar>
    <div class="my-current">
      <p>当前余额</p>
      <div>
        ￥
        <span>{{ individualAccount.je }}</span>
      </div>
    </div>
    <!-- 提现金额 -->
    <div v-if="this.$route.params.type == '1'" class="recharge-ithdraw">
      <p>充值金额</p>
      <van-cell-group>
        <van-field class="amount-money" v-model="number" type="number">
          <template #left-icon>
            <div class="amount-money">
              ￥
            </div>
          </template>
        </van-field>
      </van-cell-group>
    </div>
    <div v-if="this.$route.params.type == '2'" class="recharge-ithdraw">
      <p>提现金额</p>
      <van-cell-group>
        <van-field class="amount-money" v-model="number" type="number">
          <template #left-icon>
            <div class="amount-money">
              ￥
            </div>
          </template>
        </van-field>
      </van-cell-group>
    </div>
    <div
      v-if="this.$route.params.type == '1'"
      class="confirm-payment"
      @click="ConfirmPayment"
    >
      确认充值
    </div>
    <div
      v-if="this.$route.params.type == '2'"
      class="confirm-payment"
      @click="ConfirmPayment"
    >
      确认提现
    </div>
  </div>
</template>

<script>
import { Dialog, Toast } from "vant";
import {
  individualAccount,
  TopUp,
  WithdrawDeposit
} from "../../../assets/js/api/user";
export default {
  name: "TopUp",
  data() {
    return {
      type: "",
      individualAccount: {},
      number: ""
    };
  },
  created() {
    let dsBridge = require("dsbridge");
    dsBridge.call("goBack", "", function() {
      history.go(-1);
    });

    // this.$route.params.type  1：充值  ，2：提现
    if (this.$route.params.type == "1") {
      this.type = "充值";
    }
    if (this.$route.params.type == "2") {
      this.type = "提现";
    }

    individualAccount().then(res => {
      this.individualAccount = res.data;
    });
  },
  methods: {
    ConfirmPayment() {
      // console.log("提现");
      if (this.number == "") {
        Toast("请输入金额");
        return;
      }
      let obj = {
        je: this.number
      };
      if (this.$route.params.type == "1") {
        TopUp(obj).then(res => {
          // console.log(res);
          if (res.code == 509) {
            Dialog.confirm({
              message: "您还未实名认证，是否去实名认证"
            })
              .then(() => {
                // on confirm
                this.$router.push({ path: "/IdentityAuthentication" });
              })
              .catch(() => {
                // on cancel
                // console.log("取消");
              });
          }
          if (res.code == 0) {
            Toast.success("充值成功");
            this.$router.go(-1);
          }
        });
      }
      if (this.$route.params.type == "2") {
        let money = Number(this.number);
        let figure = Number(this.individualAccount.je);
        // console.log(money > figure);
        // console.log(`金额:${money};余额:${figure}`);
        if (money > figure) {
          Toast.fail("提现金额不能大于账户余额");
          return;
        }
        WithdrawDeposit(obj).then(res => {
          // console.log(res);
          if (res.code == 509) {
            Dialog.confirm({
              message: "您还未实名认证，是否去实名认证"
            })
              .then(() => {
                // on confirm
                this.$router.push({ path: "/IdentityAuthentication" });
              })
              .catch(() => {
                // on cancel
                // console.log("取消");
              });
          }
          if (res.code == 510) {
            Dialog.confirm({
              message: "您还未绑定银行卡，是否去绑定"
            })
              .then(() => {
                // on confirm
                this.$router.push({ path: "/BankCard" });
              })
              .catch(() => {
                // on cancel
                // console.log("取消");
              });
          }
          if (res.code == 511) {
            Toast(res.msg);
          }
          if (res.code == 0) {
            Toast.success("提现成功");
            this.$router.go(-1);
          }
        });
      }
    },

    onClickLeft() {
      this.$router.go(-1);
    }
  },
  components: {
    [Dialog.Component.name]: Dialog.Component
  }
};
</script>

<style></style>
